package uiLayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import errorLayer.*;
import uiUtil.ScheduleBlockPanel;
import uiUtil.TeachingBlockDialog;
import controlLayer.ScheduleCtrl;
import controlLayer.TeachingBlockCtrl;
import modelLayer.Schedule;
import modelLayer.Class;
import modelLayer.TeachingBlock;

public class ClassSchedule extends JPanel {
	
	private JPanel scheduleGridPanel;
	private ScheduleCtrl sc = new ScheduleCtrl();
	private TeachingBlockCtrl tbCtrl = new TeachingBlockCtrl();
	private TeachingBlockDialog tbDialog;
	private TeachingBlock tb;
	private Class cl;
	private int classId = 0;
	private ButtonGroup group = new ButtonGroup();
	private String clTeacher = "N/A";
	private List<ScheduleBlockPanel> scheduleBlocks = new ArrayList<>();
	
	public ClassSchedule(Class tempCl) {
		
		this.cl = tempCl;
		this.classId = cl.getID();
		
		setLayout(new BorderLayout(0, 0));
		
		addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent e) {
				
				loadInBlocks(sc.classScheduleBlocks(classId));
				GUISetup();
				statusBar();
				updateUI();
				
			}
			
			@Override
			public void componentResized(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentMoved(ComponentEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void componentHidden(ComponentEvent e) {
				
				removeAll();
				updateUI();
				
			}
		});
	}
	
	private void statusBar () {
		
		try {
			clTeacher = cl.getStaff().getName();
		}
		catch (Exception e) {
			System.out.println("Exception: " + e);
		}
		
		JButton addingEditingBtn = new JButton("Add / Edit");
		
		addingEditingBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				
				boolean update = false;
				
				tbDialog = new TeachingBlockDialog(sc);
				tbDialog.setVisible(true);
				
				try {
				
					sc.addTBToSchedule(classId, sc.getSelectedBlockNo(), tbDialog.getTB().getID());
					
					tb = tbDialog.getTB();
					
					update = true; //to make sure pressing 'cancel' doesn't "remove" the teachingblock in the graphics
				}
				catch (Exception e) {
					System.out.println("Exception: " + e);
				}
				
				if (update) {
					updateBlock();
				}
				
			}
		});
		
		JButton removeBtn = new JButton("Remove");
		
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				
				String deletion = "TeachingBlock number: " + sc.getSelectedBlockNo();
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if (dw.getAnswer()) {
					sc.removeTBFromSchedule(sc.getSelectedBlockNo(), classId);
				
					tb = new TeachingBlock();
				
					updateBlock();
				}
			}
		});
		
		JToolBar statusBar = new JToolBar();
		statusBar.setFloatable(false);
		statusBar.setMargin(new Insets(2, 0, 0, 0));
		
		statusBar.add(addingEditingBtn);
		statusBar.add(removeBtn);
		statusBar.addSeparator();
		statusBar.add(new JLabel("Class teacher: " + clTeacher));
		
		add(statusBar, BorderLayout.SOUTH);
		
	}
	
	private void loadInBlocks (List<Schedule> schedules) {
		
		for (int i = 0; i < schedules.size(); ++i) {
			
			scheduleBlocks.add(new ScheduleBlockPanel(schedules.get(i), this.sc, group));
			
		}
		
	}
	
	private void updateBlock () {
		
		scheduleBlocks.get((sc.getSelectedBlockNo()-1)).updateBlock(tb);
		
	}
	
	private void GUISetup () {
		
		JPanel weekDays = new JPanel();
		weekDays.setLayout(new GridLayout(1, 5, 0, 0));
		add(weekDays, BorderLayout.NORTH);
		
		weekDays.add(new JLabel("Monday"));
		weekDays.add(new JLabel("Tuesday"));
		weekDays.add(new JLabel("Wednesday"));
		weekDays.add(new JLabel("Thursday"));
		weekDays.add(new JLabel("Friday"));
		
		JPanel tapContent = new JPanel();
		tapContent.setLayout(new GridLayout(0, 5, 0, 0));
		add(tapContent, BorderLayout.CENTER);
		
		JPanel mondayPanel = new JPanel();
		mondayPanel.setLayout(new GridLayout(6, 1, 0, 0));
		tapContent.add(mondayPanel);
		
		JPanel tuesdayPanel = new JPanel();
		tuesdayPanel.setLayout(new GridLayout(6, 1, 0, 0));
		tapContent.add(tuesdayPanel);
		
		JPanel wedensdayPanel = new JPanel();
		wedensdayPanel.setLayout(new GridLayout(6, 1, 0, 0));
		tapContent.add(wedensdayPanel);
		
		JPanel thursdayPanel = new JPanel();
		thursdayPanel.setLayout(new GridLayout(6, 1, 0, 0));
		tapContent.add(thursdayPanel);
		
		JPanel fridayPanel = new JPanel();
		fridayPanel.setLayout(new GridLayout(6, 1, 0, 0));
		tapContent.add(fridayPanel);
		
		for (int i = 0; i < scheduleBlocks.size(); ++i) {
			
			if (i < 6) {
				
				mondayPanel.add(scheduleBlocks.get(i));
				
			}
			else if (i < 12) {
				
				tuesdayPanel.add(scheduleBlocks.get(i));
				
			}
			else if (i < 18)  {
				
				wedensdayPanel.add(scheduleBlocks.get(i));
				
			}
			else if (i < 24) {
				
				thursdayPanel.add(scheduleBlocks.get(i));
				
			}
			else if (i < 30){
				//This could technically be a simple else however it would cause trouble when reloading in the same sesion
				
				fridayPanel.add(scheduleBlocks.get(i));
				
			}
			
		}
		
	}
	
}
